package com.hotel.comm.bean.lucene;

import com.hotel.comm.bean.EnumConstants.*;
import com.hotel.comm.bean.company.EjobCompany;
import com.hotel.comm.bean.company.EjobCompanyPosition;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.document.*;
import org.apache.lucene.index.Term;

import java.io.Serializable;
import java.util.Date;

public class PositionIndexBean implements IndexableBean, Serializable, Cloneable {
    /**
     *
     */
    private static final long serialVersionUID = 32491732469132L;
    /**
     * 职位Id
     */
    private Long positionId;
    /**
     * 职位名称
     */
    private String positionName;
    /**
     * 职位类型
     */
    private PositionType positionType;
    /**
     * 工作年限
     */
    private WorkingLife workingLife;
    /**
     * 招聘人数
     */
    private String recruitNum;
    /**
     * 公司Id
     */
    private Long companyId;
    /**
     * 公司名称
     */
    private String companyName;
    /**
     * 企业评分<br/>
     * 冗余字段
     */
    private Integer companyScore;
    /**
     * 公司性质
     */
    private CompanyNature companyNature;
    /**
     * 公司规模
     */
    private CompanySize companySize;
    /**
     * 职位薪水下限
     */
    private Integer salaryLow;
    /**
     * 职位薪水上限
     */
    private Integer salaryHight;
    /**
     * 部门Id
     */
    private Long departmentId;
    /**
     * 部门名称
     */
    private String departmentName;
    /**
     * 城市Id
     */
    private Long cityId;
    /**
     * 城市名称
     */
    private String cityName;
    /**
     * 学历要求
     */
    private EducationRequirements educationRequirements;
    /**
     * 是否上线<br/>
     * 职位修改必须在下线状态才能修改
     */
    private IsOnline isOnline;
    /**
     * 是否置顶
     */
    private IsTop isTop;
    /**
     * 操作员
     */
    private Long operatorId;
    /**
     * 刷新日期<br>
     * 初始化创建时，刷新日期为创建日期<br/>
     * 更新后，刷新日期为上线时的日期(更新需先下线)<br>
     * 企业刷新后，同时更新发布的职位日期
     */
    private Date refleshDate;
    /**
     * 存储动作类型
     */
    private ActionType actionType;

    /**
     * 默认构造函数
     */
    public PositionIndexBean() {
    }

    /**
     * 构造函数
     *
     * @param position
     * @param company
     */
    public PositionIndexBean(EjobCompanyPosition position, EjobCompany company) {
        this.positionId = position.getId();
        this.positionName = position.getPositionName();
        this.positionType = position.getPositionType();
        this.workingLife = position.getWorkingLife();
        this.recruitNum = position.getRecruitNum();
        this.companyId = company.getId();
        this.companyName = company.getCompanyName();
        this.companyScore = company.getCompanyScore();
        this.companyNature = company.getCompanyNature();
        this.companySize = company.getCompanySize();
        this.salaryLow = position.getSalaryLow();
        this.salaryHight = position.getSalaryHight();
        this.departmentId = position.getEjobDepartment().getId();
        this.departmentName = position.getEjobDepartment().getDepartmentName();
        this.cityId = position.getEjobCity().getId();
        this.cityName = position.getEjobCity().getCityName();
        this.educationRequirements = position.getEducationRequirements();
        this.isOnline = position.getIsOnline();
        this.isTop = position.getIsTop();
        this.operatorId = position.getOperatorId();
        this.refleshDate = position.getRefleshDate();
    }

    public Long getPositionId() {
        return positionId;
    }

    public void setPositionId(Long positionId) {
        this.positionId = positionId;
    }

    public String getPositionName() {
        return positionName;
    }

    public void setPositionName(String positionName) {
        this.positionName = positionName;
    }

    public PositionType getPositionType() {
        return positionType;
    }

    public void setPositionType(PositionType positionType) {
        this.positionType = positionType;
    }

    public WorkingLife getWorkingLife() {
        return workingLife;
    }

    public void setWorkingLife(WorkingLife workingLife) {
        this.workingLife = workingLife;
    }

    public String getRecruitNum() {
        return recruitNum;
    }

    public void setRecruitNum(String recruitNum) {
        this.recruitNum = recruitNum;
    }

    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getCompanyScore() {
        return companyScore;
    }

    public void setCompanyScore(Integer companyScore) {
        this.companyScore = companyScore;
    }

    public CompanyNature getCompanyNature() {
        return companyNature;
    }

    public void setCompanyNature(CompanyNature companyNature) {
        this.companyNature = companyNature;
    }

    public CompanySize getCompanySize() {
        return companySize;
    }

    public void setCompanySize(CompanySize companySize) {
        this.companySize = companySize;
    }

    public Integer getSalaryLow() {
        return salaryLow;
    }

    public void setSalaryLow(Integer salaryLow) {
        this.salaryLow = salaryLow;
    }

    public Integer getSalaryHight() {
        return salaryHight;
    }

    public void setSalaryHight(Integer salaryHight) {
        this.salaryHight = salaryHight;
    }

    public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public Long getCityId() {
        return cityId;
    }

    public void setCityId(Long cityId) {
        this.cityId = cityId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public EducationRequirements getEducationRequirements() {
        return educationRequirements;
    }

    public void setEducationRequirements(EducationRequirements educationRequirements) {
        this.educationRequirements = educationRequirements;
    }

    public IsOnline getIsOnline() {
        return isOnline;
    }

    public void setIsOnline(IsOnline isOnline) {
        this.isOnline = isOnline;
    }

    public IsTop getIsTop() {
        return isTop;
    }

    public void setIsTop(IsTop isTop) {
        this.isTop = isTop;
    }

    public Long getOperatorId() {
        return operatorId;
    }

    public void setOperatorId(Long operatorId) {
        this.operatorId = operatorId;
    }

    public Date getRefleshDate() {
        return refleshDate;
    }

    public void setRefleshDate(Date refleshDate) {
        this.refleshDate = refleshDate;
    }

    @Override
    public ActionType getActionType() {
        return this.actionType;
    }

    public void setActionType(ActionType actionType) {
        this.actionType = actionType;
    }

    @Override
    public Document getDoc() {
        Document doc = new Document();
        doc.add(new StringField("positionId", this.positionId + "", Field.Store.YES));
        doc.add(new TextField("positionName", this.positionName, Field.Store.YES));
        doc.add(new TextField("positionType", this.positionType.name(), Field.Store.YES));
        doc.add(new StringField("workingLife", this.workingLife.name(), Field.Store.YES));
        doc.add(new TextField("recruitNum", this.recruitNum, Field.Store.YES));
        doc.add(new LongField("companyId", this.companyId, Field.Store.YES));
        doc.add(new TextField("companyName", this.companyName, Field.Store.YES));
        doc.add(new IntField("companyScore", this.companyScore, Field.Store.YES));
        doc.add(new StringField("companyNature", this.companyNature.name(), Field.Store.YES));
        doc.add(new StringField("companySize", this.companySize.name(), Field.Store.YES));
        doc.add(new IntField("salaryLow", this.salaryLow, Field.Store.YES));
        doc.add(new IntField("salaryHight", this.salaryHight, Field.Store.YES));
        doc.add(new LongField("departmentId", this.departmentId, Field.Store.YES));
        doc.add(new StringField("departmentName", this.departmentName, Field.Store.YES));
        doc.add(new StringField("cityId", this.cityId + "", Field.Store.YES));
        doc.add(new TextField("cityName", this.cityName, Field.Store.YES));
        doc.add(new StringField("educationRequirements", this.educationRequirements.name(), Field.Store.YES));
        doc.add(new StringField("isOnline", this.isOnline.name(), Field.Store.YES));
        doc.add(new StringField("isTop", this.isTop.name(), Field.Store.YES));
        doc.add(new LongField("refleshDate", this.refleshDate.getTime(), Field.Store.YES));
        return doc;
    }

    @Override
    public String toString() {
        return new GsonBuilder().create().toJson(this);
    }

    @Override
    public IndexType getType() {
        return IndexType.INDEX_POSITION;
    }

    @Override
    public Term getTerm() {
        return new Term("positionId", this.positionId + "");
    }

    @Override
    public IndexableBean parse(Document doc) {
        PositionIndexBean position = new PositionIndexBean();
        position.setPositionId(Long.parseLong(doc.get("positionId")));
        position.setPositionName(doc.get("positionName"));
        position.setPositionType(PositionType.getByName(doc.get("positionType")));
        position.setWorkingLife(WorkingLife.getByName(doc.get("workingLife")));
        position.setRecruitNum(doc.get("recruitNum"));
        position.setCompanyId(StringUtils.isBlank(doc.get("companyId")) ? 0 : Long.parseLong(doc.get("companyId")));
        position.setCompanyName(doc.get("companyName"));
        position.setCompanyScore(StringUtils.isBlank(doc.get("companyScore")) ? 0 : Integer.parseInt(doc.get("companyScore")));
        position.setSalaryLow(StringUtils.isBlank(doc.get("salaryLow")) ? 0 : Integer.parseInt(doc.get("salaryLow")));
        position.setSalaryHight(StringUtils.isBlank(doc.get("salaryHight")) ? 0 : Integer.parseInt(doc.get("salaryHight")));
        position.setCompanyNature(CompanyNature.getByName(doc.get("companyNature")));
        position.setCompanySize(CompanySize.getByName(doc.get("companySize")));
        position.setDepartmentId(StringUtils.isBlank(doc.get("departmentId")) ? 0 : Long.parseLong(doc.get("departmentId")));
        position.setDepartmentName(doc.get("departmentName"));
        position.setCityId(StringUtils.isBlank(doc.get("cityId")) ? 0 : Long.parseLong(doc.get("cityId")));
        position.setCityName(doc.get("cityName"));
        position.setEducationRequirements(EducationRequirements.getByName(doc.get("educationRequirements")));
        position.setIsOnline(IsOnline.getByName(doc.get("isOnline")));
        position.setIsTop(IsTop.getByName(doc.get("isTop")));
        position.setRefleshDate(StringUtils.isBlank(doc.get("refleshDate")) ? null : new Date(Long.parseLong(doc.get("refleshDate"))));
        return position;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
